clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use if qruf1<. using $Work/EBR_new_monthly, clear
tsset permno datem

* compute actual returns
generate   retQr1=1
forvalues x=1(1)5 {
generate   retYr`x'=1
}

generate  retMo1=ret_mo1
forvalues x=1(1)3 {
replace   retQr1=retQr1*(ret_mo`x')
}
forvalues x=1(1)12 {
replace   retYr1=retYr1*(ret_mo`x')
}
forvalues x=13(1)24 {
replace   retYr2=retYr2*(ret_mo`x')
}
forvalues x=25(1)36 {
replace   retYr3=retYr3*(ret_mo`x')
}
forvalues x=37(1)48 {
replace   retYr4=retYr4*(ret_mo`x')
}
forvalues x=49(1)60 {
replace   retYr5=retYr5*(ret_mo`x')
}
* *************************
scalar alfa= 0.97749304
gen cumRetNewMo1=ln(retMo1) 
gen cumRetNewQr1=ln(retQr1) 
gen cumRetNewYr1=ln(retYr1) 
gen cumRetNewYr3=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3) 
gen cumRetNewYr5=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3)+alfa^3*ln(retYr4)+alfa^4*ln(retYr5)
* **************************
gen cumQNRetMo1=ln(1+qnic1 ) 
gen cumQNRetQr1=ln(qnic_qr1) 
gen cumQNRetYr1=ln(qnic_yr1) 
gen cumQNRetYr3=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3) 
gen cumQNRetYr5=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3)+alfa^3*ln(qnic_yr4)+alfa^4*ln(qnic_yr5)
gstats winsor cumQN*, cuts(1 99) by(datem) replace 
* **************************
keep permco permno datem cum*
tempfile cumQ
save "`cumQ'", replace
* **************************
merge 1:1 permco permno datem using $Work/Firms01_June
keep if _merge==3
drop _merge
* *****************************************************
keep if BOOK+SIZE<.
* *****************************************************
generate EBR=cumQNRetMo1
eststo:  quietly: ivreghdfe cumRetNewMo1 EBR, dkraay(1) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewMo1 EBR, dkraay(1)
*
replace EBR=cumQNRetQr1
eststo:  quietly: ivreghdfe cumRetNewQr1 EBR, dkraay(4) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewQr1 EBR, dkraay(4)
*
replace EBR=cumQNRetYr1
eststo:  quietly: ivreghdfe cumRetNewYr1 EBR, dkraay(13) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr1 EBR, dkraay(13)
*
replace EBR=cumQNRetYr3
eststo:  quietly: ivreghdfe cumRetNewYr3 EBR, dkraay(37) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr3 EBR, dkraay(37)
*
replace EBR=cumQNRetYr5
eststo:  quietly: ivreghdfe cumRetNewYr5 EBR, dkraay(61) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr5 EBR, dkraay(61)
* *****************************************************
local file "$Tables/AppendixB8_June.csv"
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) replace
